/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.cruisewatcher;

import com.intellij.openapi.options.ConfigurationException;

import javax.swing.*;
import java.awt.*;

public class CruiseWatcherConfigurationPanel extends JPanel {

    private CruiseWatcherSettings settings;
    private JTextField baseUrl;
    private JTextField resultUrl;
    private JTextField baseImageFolder;
    private JTextField fixedImageFolder;
    private JTextField refreshInterval;
    private JCheckBox disableProxy;
    private CruiseWatcherTableModel tableModel;

    public CruiseWatcherConfigurationPanel(CruiseWatcherSettings settings) {
        this.settings = settings;
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        setMinimumSize(new Dimension(400, 0));
        c.insets = new Insets(5, 5, 5, 5);
        c.gridx = 0;
        c.gridy = 0;
        add(new JLabel("Base URL"), c);
        c.gridy = 1;
        add(new JLabel("Base Image Folder"), c);
        c.gridy = 2;
        add(new JLabel("Refresh Interval (mins)"), c);
        c.gridy = 3;
        add(new JLabel("Don't use proxy"), c);
        c.gridy = 4;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        c.gridwidth = 2;
        tableModel = new CruiseWatcherTableModel(settings.getBuilds());
        add(new CruiseWatcherConfigurationTablePanel(tableModel), c);
        c.gridx = 1;
        c.gridy = 0;
        add(baseUrl = new JTextField(settings.baseUrl), c);
        c.gridy = 1;
        add(baseImageFolder = new JTextField(settings.baseImageFolder), c);
        c.gridy = 2;
        add(refreshInterval = new JTextField(String.valueOf(settings.refreshInterval)), c);
        c.gridy = 3;
        add(disableProxy = new JCheckBox(), c);
    }

    /**
     * Helper for development, so we can test the gui without restarting IDEA.
     */
    public static void main(String[] args) {
        JFrame f = new JFrame();
        CruiseWatcherSettings config = new CruiseWatcherSettings();

        JPanel panel = new CruiseWatcherConfigurationPanel(config);
        f.getContentPane().add(panel);
        f.pack();
        f.setVisible(true);
    }

    public void reset() {
        baseUrl.setText(settings.baseUrl);
        baseImageFolder.setText(settings.baseImageFolder);
        refreshInterval.setText(String.valueOf(settings.refreshInterval));
        disableProxy.setSelected(true);
        tableModel.setBuilds(settings.getBuilds());
    }

    public void apply() throws ConfigurationException {
        try {
            settings.baseUrl = baseUrl.getText();
            settings.baseImageFolder = baseImageFolder.getText();
            settings.refreshInterval = Integer.parseInt(refreshInterval.getText());
            settings.disableProxy = disableProxy.isSelected();
            settings.setBuilds(tableModel.getBuilds());
            tableModel.resetDirty();
        }
        catch (Exception e) {
            throw new ConfigurationException(e.getMessage());
        }
    }

    public boolean isModified() {
        return !baseUrl.getText().equals(settings.baseUrl) ||
                !baseImageFolder.getText().equals(settings.baseImageFolder) ||
                !refreshInterval.getText().equals(String.valueOf(settings.refreshInterval)) ||
                disableProxy.isSelected() != settings.disableProxy || tableModel.isDirty();
    }
}
